<template>
  <div class="app-container calendar-list-container">
    <el-tabs type="border-card" v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="订单初审" name="first" v-if="per === '1' || per === '3'">
        <component :is="allList" :list="list" :listLoading="listLoading" v-on:get-order="getOrder"></component>
      </el-tab-pane>
      <el-tab-pane label="订单复审" name="second" v-if="per === '2' || per === '3'">
        <component :is="allList" :list="list" :listLoading="listLoading" v-on:get-order="getOrder"></component>
      </el-tab-pane>
    </el-tabs>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page"
        :page-sizes="[5,10,20,30, 50]" :page-size="listQuery['per-page']" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { firstList, secondList, getFirstOrder, getSecondOrder } from '@/api/order'
import { parseTime, hasOrderPermission } from '@/utils'
import alllist from './components/alllist' // 订单列表页

export default {
  name: 'complexTable',
  data() {
    return {
      allList: alllist,
      activeName: 'first',
      tableKey: 0,
      list: null,
      total: null,
      per: '3',
      listLoading: true,
      listQuery: {
        page: 1,
        'per-page': 10,
        importance: undefined,
        title: undefined,
        type: undefined
      }
    }
  },
  created() {
    // 获取初审复审权限
    this.per = hasOrderPermission()
    if (this.per === '1') {
      this.activeName = 'first'
    } else if (this.per === '2') {
      this.activeName = 'second'
    } else if (this.per === '3') {
      this.activeName = 'first'
    }
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      // 判断是待初审还是待复审
      if (this.activeName === 'first') {
        firstList(this.listQuery).then(response => {
          this.list = response.data.data
          this.total = response.data._meta.totalCount
          this.listLoading = false
        })
      } else {
        secondList(this.listQuery).then(response => {
          this.list = response.data.data
          this.total = response.data._meta.totalCount
          this.listLoading = false
        })
      }
    },
    handleClick() {
      this.resetTemp()
      this.getList()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery['per-page'] = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    getOrder: function(str) { // 领取订单
      if (str === '') {
        this.$alert('请先勾选想要领取的工单', '提示', {
          confirmButtonText: '确定',
          type: 'warning'
        }).then(() => {
        })
      } else {
        // 声明变量
        const param = {}
        param.oids = str
        const tempData = Object.assign({}, param)
        // 如果是待初审
        if (this.activeName === 'first') {
          getFirstOrder(tempData).then(() => {
            this.$message({
              type: 'success',
              message: '领取成功!',
              duration: 2000,
              onClose: () => {
                this.getList()
              }
            })
          })
        } else { // 如果是待复审
          getSecondOrder(tempData).then(() => {
            this.$message({
              type: 'success',
              message: '领取成功!',
              duration: 2000,
              onClose: () => {
                this.getList()
              }
            })
          })
        }
      }
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        status: 'published',
        type: ''
      }
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    }
  }
}
</script>
